package com.granada.controller.pc;

import com.granada.behaviour.neutral.UserRoleWithNeutralBehavior;
import com.granada.behaviour.pc.append.UserWithPingBehavior;
import com.granada.service.base.UserService;
import com.granada.service.ping.UserWithPingService;
import com.granada.utils.DataResult;
import com.granada.aop.annotation.LogAnnotation;
import com.granada.exception.BusinessException;
import com.granada.exception.code.BaseResponseCode;
import com.granada.model.pc.UserWithPing;
import com.granada.vo.resp.UserOwnRoleRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/pc")
@RestController
@Api(tags = "平台端-角色管理")
public class PcPingRoleAppendController {
    @GetMapping("/ping/user/roles/{userId}")
    @ApiOperation(value = "赋予角色-获取所有角色接口")
    @LogAnnotation(title = "pc-平台用户管理",action = "赋予角色-获取所有角色接口")
    @RequiresPermissions("sys:ping:roles:all:by:user:id")
    public DataResult<UserOwnRoleRespVO> getUserOwnRole(@PathVariable("userId")Long userId) throws Exception {
        DataResult<UserOwnRoleRespVO> result= DataResult.success();
        UserService userService = new UserWithPingService(
                new UserWithPing(
                        null,
                        null,
                        null,
                        null,
                        null
                ),
                new UserWithPingBehavior(),
                null,
                null,
                new UserRoleWithNeutralBehavior(),
                null
        );
        result.setData(userService.getUserOwnRole(userId));
//        result.setData(userService.getPingUserOwnRole(userId));
        return result;
    }

    @PutMapping("/ping/user/roles/{userId}")
    @ApiOperation(value = "赋予角色-用户赋予角色接口")
    @LogAnnotation(title = "用户管理",action = "赋予角色-用户赋予角色接口")
    @RequiresPermissions("sys:ping:roles:update:by:user:id")
    public DataResult<UserOwnRoleRespVO> setUserOwnRole(@PathVariable("userId")Long userId, @RequestBody List<Long> roleIds) throws Exception {
        DataResult result=DataResult.success();
        if (roleIds.size()>1){
            throw new BusinessException(BaseResponseCode.GIVE_A_ROLE);
        }
        UserService userService = new UserWithPingService(
                new UserWithPing(
                        null,
                        null,
                        null,
                        null,
                        null
                ),
                new UserWithPingBehavior(),
                null,
                null,
                new UserRoleWithNeutralBehavior(),
                null
        );
        userService.setUserOwnRole(userId,roleIds);
        return result;
    }

}
